package com.indra.pe.bbva.bp.in.helper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import com.indra.pe.bbva.bp.in.util.UtilGeneric;
import com.indra.pe.bbva.bp.in.util.UtilHelper;

public class SelectItemHelper<E> implements Serializable{
 
	private static final long serialVersionUID = -644288279898861227L;
	
	private String[] values;
	private String[] labels;	
	
	
	
	public SelectItemHelper(String[] values, String[] labels) {
		this.values = values;
		this.labels = labels;
	}


	public List<SelectItem> factorySelectItemWithLabelAll(List<E> list) throws Exception{
		List<SelectItem> listSelectItem = new ArrayList<SelectItem>();
		listSelectItem.add(new SelectItem(UtilHelper.VALUE_ALL,UtilHelper.LABEL_ALL));
		for (E e : list) {
			listSelectItem.add(new SelectItem(combinate(values,e),combinate(labels,e)));
		}
		return listSelectItem;
	}
	
	public List<SelectItem> factorySelectItemError(){
		List<SelectItem> listError = new ArrayList<SelectItem>();
		listError.add(new SelectItem(UtilHelper.VALUE_ERROR,UtilHelper.LABEL_ERROR));
		return listError;
	}
 
	
	private String combinate(String[] labels, E e) throws Exception{
		StringBuffer stringBuffer = new StringBuffer();
		for (String label : labels) {
			stringBuffer.append(UtilGeneric.invokeMethodGet(e.getClass(),UtilGeneric.getNameMethodGET(label),e));
			stringBuffer.append(" ");
		}
		return stringBuffer.toString().trim();
	}
	
}
